using UnityEngine;

namespace EntityKit.Runtime
{
    /// <summary>
    /// Quaternion型可绑定属性
    /// </summary>
    public class QuaternionProperty : BindableProperty<Quaternion>
    {
        private float mThreshold = 0.001f;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="defaultValue">默认值</param>
        public QuaternionProperty(Quaternion defaultValue = default) : base(defaultValue)
        {
            WithComparer((a, b) => 
                Mathf.Abs(a.x - b.x) < mThreshold && 
                Mathf.Abs(a.y - b.y) < mThreshold && 
                Mathf.Abs(a.z - b.z) < mThreshold && 
                Mathf.Abs(a.w - b.w) < mThreshold);
        }

        /// <summary>
        /// 设置比较阈值
        /// </summary>
        /// <param name="threshold">新的阈值</param>
        /// <returns>当前实例</returns>
        public QuaternionProperty WithThreshold(float threshold)
        {
            mThreshold = Mathf.Max(0, Mathf.Abs(threshold));
            WithComparer((a, b) => 
                Mathf.Abs(a.x - b.x) < mThreshold && 
                Mathf.Abs(a.y - b.y) < mThreshold && 
                Mathf.Abs(a.z - b.z) < mThreshold && 
                Mathf.Abs(a.w - b.w) < mThreshold);
            return this;
        }
    }
}